package com.adong.rabbitmq.work;

import com.adong.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class WorkConsumer2 {
    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();
        // 创建频道
        Channel channel = connection.createChannel();
        // 声明队列
        String QUEUE_NAME = "work_queue";

        //一次只能接收并处理一个消息
        channel.basicQos(1);
        // 创建消费者;并设置消息处理
        Consumer consumer = new DefaultConsumer(channel) {
            /*
             * consumerTag 消息者标签，在channel.basicConsume时候可以指定
             * envelope 消息包的内容，可从中获取消息id，消息routingkey，交换机，消息和重传标志(收到消息失败后是否需要重新发送)
             * properties 属性信息
             * body 消息
             */

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body：" + new String(body));
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

}
